/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk> and Matthias Buch-Kromann <mbk.isv@cbs.dk>
 *
 *  This file is part of the IncrementalParser package.
 *
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.incparser.data;

import java.util.List;
import org.osdtsystem.incparser.graphs.BaseGraph;
import org.osdtsystem.incparser.graphs.ConllAdapter;

/**
 *
 * @author Martin Haulrich and Matthias Buch-Kromann
 */
public class CONLLSentence {
    public static final String NEWLINE = System.getProperty("line.separator");
    public List<CONLLToken> words;

    public CONLLSentence(List<CONLLToken> words) {
        this.words = words;
    }

    public int size() {
        return words.size();
    }

    public CONLLToken get(int i) {
        return (i == -1) ? CONLLToken.ROOT :  words.get(i);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CONLLSentence other = (CONLLSentence) obj;
        if (this.words != other.words && (this.words == null || !this.words.equals(other.words))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.words != null ? this.words.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (CONLLToken w : words) {
            sb.append(w.toString());
            sb.append(NEWLINE);
        }
        return sb.toString();
    }

    public String toTokenString() {
        StringBuilder sb = new StringBuilder();
        for (CONLLToken w : words) {
            sb.append(w.get(CONLLToken.FORM));
            sb.append(" ");
        }
        return sb.toString();
    }

    public void writeToGraph(ConllAdapter graph) {
        // Clear graph
        graph.clear();
        graph.setConllSentence(this);
        BaseGraph baseGraph = graph.baseGraph();

        // Add nodes and features
        int[] nodes = new int[words.size()];
        for (int nodeID = 0; nodeID < nodes.length; ++nodeID) {
            nodes[nodeID] = graph.addNode();
            CONLLToken tok = words.get(nodeID);

            // Integer features
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.ID, Integer.parseInt(tok.get(CONLLToken.ID)));
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.FORM, graph.stringID(tok.get(CONLLToken.FORM)));
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.LEMMA, graph.stringID(tok.get(CONLLToken.LEMMA)));
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.CPOSTAG, graph.stringID(tok.get(CONLLToken.CPOSTAG)));
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.POSTAG, graph.stringID(tok.get(CONLLToken.POSTAG)));

            // Object features
            String[] featsStrings = tok.get(CONLLToken.FEATS).split("\\|");
            int[] featsInts = new int[featsStrings.length];
            for (int f = 0; f < featsStrings.length; ++f)
                featsInts[f] = graph.stringID(featsStrings[f]);
            graph.setObjectFeature(baseGraph, nodes[nodeID], graph.FEATS, featsInts);
        }

        // Add edges
        for (int nodeID = 0; nodeID < nodes.length; ++nodeID) {
            // Extract gold heads and dependency relations
            CONLLToken tok = words.get(nodeID);
            int node = nodes[nodeID];
            int headID = Integer.parseInt(tok.get(CONLLToken.HEAD));
            int pheadID = Integer.parseInt(tok.get(CONLLToken.HEAD));
            int head = headID > 0 ? nodes[headID - 1] : 0;
            int deprel = graph.typeID(tok.get(CONLLToken.DEPREL));
            int pdeprel = graph.typeID(tok.get(CONLLToken.PDEPREL));

            // Add gold int features
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.HEAD, headID);
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.PHEAD, pheadID);
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.DEPREL, deprel);
            graph.setIntFeature(baseGraph, nodes[nodeID], graph.PDEPREL, pdeprel);

            // Add edges
            graph.addEdge(node, head, deprel);
        }
    }
}
